home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-09-01 | 9.7 KB | 332 lines | [TEXT/PJMM] |
- {****************************************************}
- {}
- { TestTherm.p }
- { A small program to demonstrate CThermometer }
- { WARNING: Don't use this program as a example of good programming }
- { practice, it's a hack }
- { Copyright © 1990, Captain Mac Enterprises. All rights reserved. }
- { 8/23/90 }
- {}
- {****************************************************}
- program TestTherm;
- uses
- Script, MiniIntf, CThermometer;
- const
- quitItem = 1;
- barItem = 2;
- pieItem = 3;
- incrItem = 4;
- menuItem = 5;
- durationItem = 9;
- ckBoxTicks = 10;
- aboutItem = 11;
- ckBoxUseMsg = 12;
- msgItem = 13;
-
- menuID = 1000;
-
- loopStart = 1;
-
-
- var
- gFillPat: Pattern;
- gIncrement, gLoopEnd: Integer;
- gUseTicks, gUseMsg: Boolean;
- gMsg: Str255;
-
-
- procedure DoCBarTherm (msg: Str255);
- var
- i: Integer;
- aThermometer: CBarTherm;
- begin
- New(aThermometer); {create the object}
- aThermometer.IThermometer(msg, gIncrement, gFillPat, gUseTicks); {init therm}
- for i := loopStart to gLoopEnd do {get something to indicate}
- if aThermometer.AdjThermometer(Round(i / gLoopEnd * 100)) then {send a % and check for cancel or end of loop}
- Leave;
- aThermometer.Free; {get rid of the object}
- end;
-
-
- procedure DoCPieTherm (msg: Str255);
- var
- i: Integer;
- aThermometer: CPieTherm;
- begin
- New(aThermometer); {create the object}
- aThermometer.IThermometer(msg, gIncrement, gFillPat, gUseTicks); {init therm}
- for i := loopStart to gLoopEnd do {get something to indicate}
- if aThermometer.AdjThermometer(Round(i / gLoopEnd * 100)) then {send a % and check for cancel or end of loop}
- Leave;
- aThermometer.Free; {get rid of the object}
- end;
-
-
- procedure CenterWindow (theWindow: WindowPtr);
- var
- h, v: Integer;
- begin {CenterWindow}
- with theWindow^, portRect do
- begin
- h := (screenBits.bounds.right - screenBits.bounds.left) div 2 - (right - left) div 2;
- v := GetMBarHeight div 2 + (screenBits.bounds.bottom - screenBits.bounds.top) div 2 - (bottom - top) div 2;
- end;
- MoveWindow(theWindow, h, v, True);
- end; {CenterWindow}
-
-
- procedure DoCheckBox (theDialog: dialogPtr; theCkBox: Integer); {handle check box}
- var
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- p: Point;
- begin {DoCheckBox}
- GetMouse(p);
- GetDItem(theDialog, theCkBox, itemKind, itemHandle, itemRect);
- if (TrackControl(ControlHandle(itemHandle), p, nil) = inCheckBox) & (itemKind = chkCtrl + ctrlItem) then
- SetCtlValue(ControlHandle(itemHandle), 1 - GetCtlValue(ControlHandle(itemHandle))); {set new control value}
- end; {DoCheckBox}
-
-
- procedure DoPopUp (theDialog: DialogPtr; dlgItem: Integer); {popup menu for arrowheads}
- const
- patWhite = 1;
- patLtGray = 2;
- patGray = 3;
- patDkGray = 4;
- patBlack = 5;
- mTitleItem = 7;
- var
- savePort: GrafPtr;
- itemHandle: Handle;
- itemRect: Rect;
- theChoice: Longint;
- menuItem, prevPopItem, i, itemKind: Integer;
- popUpMenuH: MenuHandle; {these popups will be created on the fly}
- p: Point;
- patStr, itemStr: Str255;
- begin {DoPopUp}
- GetPort(savePort);
- SetPort(theDialog);
- popUpMenuH := GetMenu(menuID); {load the menu}
- GetDItem(theDialog, dlgItem, itemKind, itemHandle, itemRect); {get item clicked in}
- GetIText(itemHandle, patStr);
- for i := 1 to CountMItems(popUpMenuH) do
- begin
- GetItem(popUpMenuH, i, itemStr);
- if itemStr = patStr then
- prevPopItem := i;
- end;
- GetDItem(theDialog, mTitleItem, itemKind, itemHandle, itemRect); {get item clicked in}
- SetPt(p, itemRect.left + (itemRect.right - itemRect.left) + 4, itemRect.top); {offset where menu will appear}
- LocalToGlobal(p); {convert to global for menu manager}
- for i := 1 to CountMItems(popUpMenuH) do
- CheckItem(popUpMenuH, i, False); {uncheck all items}
- CheckItem(popUpMenuH, prevPopItem, True);
- InvertRect(itemRect); {invert item}
- InsertMenu(popUpMenuH, -1); {bring up menu as heir}
- theChoice := PopUpMenuSelect(popUpMenuH, p.v, p.h, prevPopItem); {bring up menu}
- menuItem := LoWord(theChoice); {get the number of the menu item chosen}
- if menuItem = 0 then {user chose nothing}
- InvertRect(itemRect)
- else
- begin {process menu choice}
- InvertRect(itemRect);
- CheckItem(popUpMenuH, menuItem, True);
- GetDItem(theDialog, dlgItem, itemKind, itemHandle, itemRect); {get item clicked in}
- GetItem(popUpMenuH, menuItem, itemStr);
- SetIText(itemHandle, itemStr);
- DeleteMenu(menuID); {trash the menu}
- case menuItem of
- patWhite:
- gFillPat := white;
- patLtGray:
- gFillPat := ltGray;
- patGray:
- gFillPat := gray;
- patDkGray:
- gFillPat := dkGray;
- patBlack:
- gFillPat := black;
- otherwise
- end; {case}
- end;
- ReleaseResource(Handle(popUpMenuH));
- SetPort(savePort);
- end; {DoPopUp}
-
-
- procedure GetInput (theDialog: DialogPtr);
- var
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- incrStr: Str255;
- tempLong: Longint;
- begin {GetInput}
- GetDItem(theDialog, incrItem, itemKind, itemHandle, itemRect);
- GetIText(itemHandle, incrStr);
- StringToNum(incrStr, tempLong);
- gIncrement := tempLong;
-
- GetDItem(theDialog, durationItem, itemKind, itemHandle, itemRect);
- GetIText(itemHandle, incrStr);
- StringToNum(incrStr, tempLong);
- gLoopEnd := tempLong;
- if gLoopEnd <= 0 then
- gLoopEnd := 1000;
-
- GetDItem(theDialog, ckBoxTicks, itemKind, itemHandle, itemRect);
- gUseTicks := Boolean(GetCtlValue(ControlHandle(itemHandle)));
- GetDItem(theDialog, ckBoxUseMsg, itemKind, itemHandle, itemRect);
- gUseMsg := Boolean(GetCtlValue(ControlHandle(itemHandle)));
- if gUseMsg then
- begin
- GetDItem(theDialog, msgItem, itemKind, itemHandle, itemRect);
- GetIText(itemHandle, gMsg);
- end;
- end; {GetInput}
-
-
- function MainLoopFilter (theDialog: DialogPtr; var myEvent: EventRecord; var itemNumber: Integer): Boolean;
- const
- returnCode = $24; {key codes to trap for. See IMV191 for proper codes}
- enterCode = $34;
- padEnterCode = $4C;
- var
- itemKind: Integer;
- itemHandle: Handle;
- itemRect: Rect;
- keyCode, chCode: longInt;
- ch: char;
- cmdDown: boolean;
- begin {MainLoopFilter}
- MainLoopFilter := False;
- itemNumber := 0;
- case myEvent.what of
- keyDown:
- begin
- MainLoopFilter := True; {keeps us in MainLoopFilter}
- keyCode := BitAnd(myEvent.message, keyCodeMask); {get the key code longint}
- keyCode := BitShift(keyCode, -8); {get the first word of the key code}
- chCode := BitAnd(myEvent.message, charCodeMask); {get the char code of the key pressed}
- ch := Chr(chCode); {convert to character}
- cmdDown := (BitAnd(myEvent.modifiers, cmdKey) <> 0); {is the command key down}
- if keyCode in [enterCode, padEnterCode] then
- itemNumber := quitItem {do OK button if above keys were pressed}
- else if not cmdDown then
- MainLoopFilter := False
- else
- begin
- case ch of
- 'q', 'Q':
- itemNumber := quitItem;
- 'b', 'B':
- itemNumber := barItem;
- 'p', 'P':
- itemNumber := pieItem;
- 't', 'T':
- begin
- GetDItem(theDialog, ckBoxTicks, itemKind, itemHandle, itemRect);
- if itemKind = chkCtrl + ctrlItem then
- SetCtlValue(ControlHandle(itemHandle), 1 - GetCtlValue(ControlHandle(itemHandle))); {set new control value}
- end;
- 'u', 'U':
- begin
- GetDItem(theDialog, ckBoxUseMsg, itemKind, itemHandle, itemRect);
- if itemKind = chkCtrl + ctrlItem then
- SetCtlValue(ControlHandle(itemHandle), 1 - GetCtlValue(ControlHandle(itemHandle))); {set new control value}
- end;
- 'a', 'A':
- itemNumber := aboutItem;
- otherwise
- end; {case ch of}
- end;
- end; {keyDown}
- activateEvt:
- begin
- GetDItem(theDialog, menuItem, itemKind, itemHandle, itemRect); {drop shadow of font dlg item}
- InsetRect(itemRect, -1, -1);
- FrameRect(itemRect);
- MoveTo(itemRect.botRight.h, itemRect.botRight.v);
- LineTo(itemRect.topLeft.h + 2, itemRect.botRight.v);
- MoveTo(itemRect.botRight.h, itemRect.botRight.v);
- LineTo(itemRect.botRight.h, itemRect.topLeft.v + 2);
- end;
- otherwise
- end; {case myEvent.what}
- end; {MainLoopFilter}
-
-
- procedure MainLoop;
- const
- DLOGID = 1001;
- var
- theDialog: DialogPtr;
- quit: Boolean;
- itemHit, saveLoopEnd: Integer;
- begin {MainLoop}
- quit := False;
- theDialog := GetNewDialog(DLOGID, nil, WindowPtr(-1));
- SetPort(theDialog);
- CenterWindow(theDialog);
- ShowWindow(theDialog);
- repeat
- ModalDialog(@MainLoopFilter, itemHit);
- case itemHit of
- quitItem:
- quit := True;
- barItem, pieItem, aboutItem:
- begin
- HideWindow(theDialog);
- GetInput(theDialog);
- ObscureCursor;
- if itemHit = barItem then
- if gUseMsg then
- DoCBarTherm(gMsg)
- else
- DoCBarTherm(Concat('This is a Bar Thermometer example.', Chr($0D), 'Press - . (period) to cancel.'))
- else if itemHit = pieItem then
- if gUseMsg then
- DoCPieTherm(gMsg)
- else
- DoCPieTherm(Concat('This is a Pie Thermometer example.', Chr($0D), 'Press - . (period) to cancel.'))
- else if itemHit = aboutItem then
- begin
- saveLoopEnd := gLoopEnd;
- gLoopEnd := 5000;
- DoCBarTherm('Optional: Send beer money ($5 - $10) to Dan Rosman, 231 Charter Oak Circle, Walnut Creek, CA, 94596.');
- gLoopEnd := saveLoopEnd;
- end;
- FlushEvents(mDownMask, 0);
- ShowWindow(theDialog);
- end;
- menuItem:
- DoPopUp(theDialog, itemHit);
- ckBoxTicks, ckBoxUseMsg:
- DoCheckBox(theDialog, itemHit);
- otherwise
- end; {case}
- until quit;
- DisposDialog(theDialog);
- end; {MainLoop}
-
-
- begin
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(nil);
- InitCursor;
-
- gFillPat := gray;
- gLoopEnd := 1000;
- gUseTicks := False;
- gUseMsg := False;
-
- MainLoop;
- end.